package com.thebox.pluginappdemo.HookLibrary;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.lang.reflect.Field;
import java.util.List;

import static com.thebox.pluginappdemo.HookLibrary.HookHelper.TARGET_INTENT;

/**
 * Created by Administrator on 2018/3/29 0029.
 */

public class HCallback implements Handler.Callback{
    public static final int LAUNCH_ACTIVITY = 100;

//    private int launchActivity = -1;

    Handler mHandler;

    public HCallback(Handler handler) {
        mHandler = handler;
//        try {
//            Class<?> innerClass = Class.forName("android.app.ActivityThread$H");
//            Field field = innerClass.getDeclaredField("LAUNCH_ACTIVITY");
//            field.setAccessible(true);
//            launchActivity = (int) field.get(null);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == LAUNCH_ACTIVITY) {
            Object r = msg.obj;
            try {
                Intent intent = (Intent) FieldUtil.getField(r.getClass(), r, "intent");
                Intent target = intent.getParcelableExtra(TARGET_INTENT);
                intent.setComponent(target.getComponent());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(msg.what == 159){
            try{
                //obj == ClientTransaction
                Object obj = msg.obj;
                //获取ClientTransaction下的mActivityCallbacks
                //private List<ClientTransactionItem> mActivityCallbacks;
                //ClientTransactionItem有个子类是LaunchActivityItem,我们需要的是这个子类
                Field mActivityCallbacksField = obj.getClass().getDeclaredField("mActivityCallbacks");
                mActivityCallbacksField.setAccessible(true);
                //获取mActivityCallbacks的实例
                List<Object> mActivityCallbacks = (List<Object>) mActivityCallbacksField.get(obj);
                Log.e("TAG", "handleMessage: mActivityCallbacks= " + mActivityCallbacks);

                if (mActivityCallbacks.size() > 0 ){
                    String luanchName = "android.app.servertransaction.LaunchActivityItem";
                    //LaunchActivityItem是ClientTransactionItem的子类
                    //  而Intent的信息则存在LaunchActivityItem的mIntent中
                    if (mActivityCallbacks.get(0).getClass().getCanonicalName().equals(luanchName)){
                        Object object = mActivityCallbacks.get(0);
                        Field intentField = object.getClass().getDeclaredField("mIntent");
                        intentField.setAccessible(true);
                        Intent intent = (Intent) intentField.get(object);
                        Intent rawIntent = intent.getParcelableExtra(TARGET_INTENT);
                        intent.setComponent(rawIntent.getComponent());

                    }
                }
            }catch (Exception e){
                Log.e("TAG", "handleMsg 159: Exception" + e.getMessage());
                e.printStackTrace();
            }
        }
        mHandler.handleMessage(msg);
        return true;
    }
}
